import java.io.*;
import java.net.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class FileSocket implements Runnable{
    
    public int port;
    public String serverAddr;
    public Socket socket;
    public ChatClient chatclient;
    public PrivateChat privatechat;
    public ObjectInputStream In;
    public ObjectOutputStream Out;
        
    public FileSocket(ChatClient parent) throws IOException{
    	chatclient = parent; 
    	this.serverAddr = chatclient.ServerName; 
    	this.port = chatclient.ServerPort;
        socket = new Socket(InetAddress.getByName(serverAddr), port);
            
        Out = new ObjectOutputStream(socket.getOutputStream());
        Out.flush();
        In = new ObjectInputStream(socket.getInputStream());
    }

    @Override
    public void run() {
        boolean keepRunning = true;
        while(keepRunning){
            try {
                FileObject fileobject = (FileObject) In.readObject();
                System.out.println("Incoming : "+fileobject.toString());
                
                if(fileobject.type.equals("upload_req")){
                    
                    if(JOptionPane.showConfirmDialog(chatclient, ("Accept '"+fileobject.content+"' from "+fileobject.sender+" ?")) == 0){
                        
                        JFileChooser jf = new JFileChooser();
                        jf.setSelectedFile(new File(fileobject.content));
                        int returnVal = jf.showSaveDialog(chatclient);
                       
                        String saveTo = jf.getSelectedFile().getPath();
                        if(saveTo != null && returnVal == JFileChooser.APPROVE_OPTION){
                            Download dwn = new Download(saveTo, chatclient);
                            Thread t = new Thread(dwn);
                            t.start();
                            send(new FileObject("upload_res", fileobject.recipient, (""+dwn.port), fileobject.sender));
                        }
                        else{
                            send(new FileObject("upload_res", fileobject.recipient, "NO", fileobject.sender));
                        }
                    }
                    else{
                        send(new FileObject("upload_res", fileobject.recipient, "NO", fileobject.sender));
                    }
                }
                else if(fileobject.type.equals("upload_res")){
                    if(!fileobject.content.equals("NO")){
                        int port  = Integer.parseInt(fileobject.content);
                        String addr = fileobject.sender;
                        
                        privatechat.CmdBrowse.setEnabled(false); 
                        privatechat.CmdSendFile.setEnabled(false);
                        Upload upl = new Upload(addr, port, privatechat.file, chatclient);
                        Thread t = new Thread(upl);
                        t.start();
                    }
                    else{
                    	chatclient.clientMsg.Warning(fileobject.sender+" rejected file request\n");
                    }
                }
            }
            catch(Exception ex) {
                keepRunning = false;             
                chatclient.thread.stop();
                
                System.out.println("Exception SocketClient run()");
                ex.printStackTrace();
            }
        }
    }
    
    public void send(FileObject fileobject){
        try {
            Out.writeObject(fileobject);
            Out.flush();
            System.out.println("Outgoing : "+fileobject.toString());
        } 
        catch (IOException ex) {
            System.out.println("Exception SocketClient send()");
        }
    }
    
    public void closeThread(Thread t){
        t = null;
    }
}
